package com.rxb.blog.common;

import lombok.Data;

/**
 * Author：rxb
 * Date：2020-01-17 11:33
 * Description：返回实体类的封装
 */
@Data
public class CommonResult<T> {
    /**
     * 返回码
     */
    private long code;

    /**
     * 返回描述信息
     */
    private String message;

    /**
     * 返回封装数据
     */
    private T data;

    protected  CommonResult(){

    }

    protected CommonResult(long code,String message,T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回(不带参数)
     */
    public static  <T> CommonResult<T> success(){
        return new CommonResult<T>(ResultCode.SUCCESS_CODE.getCode(),ResultCode.SUCCESS_CODE.getMessage(),null);
    }

    /**
     * 成功返回(带参数)
     */
    public static  <T> CommonResult<T> success(T data){
        return new CommonResult<T>(ResultCode.SUCCESS_CODE.getCode(),ResultCode.SUCCESS_CODE.getMessage(),data);
    }

    /**
     * 成功返回(带参数,带自定义返回message)
     */
    public static  <T> CommonResult<T> success(T data, String message){
        return new CommonResult<T>(ResultCode.SUCCESS_CODE.getCode(),message,data);
    }

    /**
     * 失败返回(带参数,带自定义返回message)
     */
    public static  <T> CommonResult<T> failed(IErrorCode errorCode){
        return new CommonResult<T>(errorCode.getCode(),errorCode.getMessage(),null);
    }

    /**
     * 失败返回(带参数,带自定义返回message)
     */
    public static  <T> CommonResult<T> failed(){
        return failed(ResultCode.EXCEPTION_CODE);
    }

    /**
     * 失败返回(带参数,带自定义返回message)
     */
    public static  <T> CommonResult<T> failed(String message){
        return new CommonResult<T>(ResultCode.EXCEPTION_CODE.getCode(),message,null);
    }

}
